package sk.gtug.pivoplus.malepivo.map;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import sk.gtug.pivoplus.malepivo.R;
import sk.gtug.pivoplus.server.domain.twig.TwigKrcmicka;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay;
import com.readystatesoftware.mapviewballoons.BalloonOverlayView;

/**
 * Class for maintaining pub markers on map.
 * @author biegleux
 *
 */
public class PubLocations extends BalloonItemizedOverlay<PubOverlayItem> {

	private List<PubOverlayItem> mItems = new ArrayList<PubOverlayItem>();
	private Context mContext;
	private InputMethodManager mInputMethodManager;
	private static final int[] BEER_MARKERS = {
		R.drawable.marker_beer1,
		R.drawable.marker_beer2,
		R.drawable.marker_beer3,
		R.drawable.marker_beer4,
		R.drawable.marker_beer5,
		R.drawable.marker_beer6,
	};

	public PubLocations(MapView mapView) {
		super(boundCenterBottom(mapView.getContext().getResources().
				getDrawable(BEER_MARKERS[0])), mapView);
		mContext = mapView.getContext();
		Drawable defaultMarker = mapView.getContext().getResources().
				getDrawable(BEER_MARKERS[0]);
		setBalloonBottomOffset(defaultMarker.getIntrinsicHeight());
		mContext = mapView.getContext();
		mInputMethodManager = (InputMethodManager) mContext.
				getSystemService(Context.INPUT_METHOD_SERVICE);
	}

	/**
	 * Prida krcmu do listu.
	 * @param pub
	 */
	public void addPub(TwigKrcmicka pub) {
		Double lat = pub.getLatitude();
		Double lon = pub.getLongitude();
		if (lat != null && lon != null) {
			GeoPoint geoPoint = new GeoPoint((int) (lat * 1e6), (int) (lon * 1e6));
			//TODO getName(), dalsie info, ako hviezdicky atd
			addItem(geoPoint, pub.getName(), pub.getName());
		}
	}

	public void addItem(GeoPoint location, String markerText, String snippet) {
		int resId = BEER_MARKERS[new Random().nextInt(BEER_MARKERS.length)];
		//TODO rating
		PubOverlayItem overlayItem = new PubOverlayItem(location, markerText, snippet, 1.5f);
		overlayItem.setMarker(boundCenterBottom(mContext.getResources().getDrawable(resId)));
		mItems.add(overlayItem);
		populate();
	}

	public void removeItem(int index) {
		mItems.remove(index);
		populate();
	}

	@Override
	protected PubOverlayItem createItem(int index) {
		return mItems.get(index);
	}

	@Override
	public int size() {
		return mItems.size();
	}

	@Override
	protected boolean onBalloonTap(int index, PubOverlayItem item) {
		Toast.makeText(mContext, "onBalloonTap for overlay index " + index,
				Toast.LENGTH_LONG).show();
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			mInputMethodManager.hideSoftInputFromWindow(mapView.getWindowToken(), 0);
		}
		return super.onTouchEvent(event, mapView);
	}

	@Override
	protected BalloonOverlayView<PubOverlayItem> createBalloonOverlayView() {
		return new PubBalloonOverlayView<PubOverlayItem>(getMapView().getContext(),
				getBalloonBottomOffset());
	}
}
